package com.taldh.algorithm.string;

import org.junit.Assert;
import org.junit.Test;

import java.util.ArrayDeque;
import java.util.HashMap;
import java.util.Map;
import java.util.Queue;

public class ConvertSolution {

    public String convert(String str, int numRows) {
        // 暴力法
        StringBuilder result = new StringBuilder();
        Map<Integer, Queue<Character>> collectionMap = new HashMap<>();
        int j = 0;
        for (int i = 0; i < str.length(); i++) {
            //3 7 11
            int line = 0;
            if (j < numRows) {
                line = j ++;
            } else if (j < 2 * numRows - 2){
                line = 2 * numRows - j - 2;
                j ++;
            } else {
                j = 0;
            }
            Queue<Character> lineQueue = collectionMap.get(line);
            if (lineQueue == null) {
                lineQueue = new ArrayDeque<>();
                collectionMap.put(line, lineQueue);
            }
            lineQueue.add(str.charAt(i));
        }
        for (int i = 0; i < numRows; i++) {
            Queue<Character> characters = collectionMap.get(i);
            for (Character character : characters) {
                result.append(character);
            }
        }
        return result.toString();
    }

    @Test
    public void test() {
        Assert.assertEquals("LCIRETOESIIGEDHN", convert("LEETCODEISHIRING", 3));
    }

    @Test
    public void test1() {
        Assert.assertEquals("LDREOEIIECIHNTSG", convert("LEETCODEISHIRING", 4));
    }
}